# django-locallibrary-tutorial-main/populate_sample_data.py
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'locallibrary.settings')

import django
django.setup()

from catalog.models import Author, Book, BookInstance, Genre, Language
from datetime import date

# 清空旧数据（可选）
BookInstance.objects.all().delete()  # 先删除书籍拷贝
Book.objects.all().delete()  # 再删除书籍
Author.objects.all().delete()  # 最后删除作者
Genre.objects.all().delete()
Language.objects.all().delete()

# 创建语言
lang_en = Language.objects.create(name='English')

# 创建类型
genre_fantasy = Genre.objects.create(name='Fantasy')

# 创建作者
author1 = Author.objects.create(first_name='Patrick', last_name='Rothfuss')
author2 = Author.objects.create(first_name='Ben', last_name='Bova')

# 创建书籍
book1 = Book.objects.create(title='1984', author=author1, summary="A dystopian novel by George Orwell.", isbn='9780451524935', language=lang_en)
book1.genre.add(genre_fantasy)
book2 = Book.objects.create(title="To Kill a Mockingbird", author=author2, summary="A classic novel by Harper Lee.", isbn='9780446310789', language=lang_en)
book2.genre.add(genre_fantasy)
book3 = Book.objects.create(title="The Great Gatsby", author=author1, summary="A novel by F. Scott Fitzgerald.", isbn='9780743273565', language=lang_en)
book3.genre.add(genre_fantasy)

# 创建拷贝
BookInstance.objects.create(book=book1, imprint="Published March 27th 2007 by Penguin Group DAW Hardcover", status='a')
BookInstance.objects.create(book=book1, imprint="Published March 27th 2007 by Penguin Group DAW Hardcover", status='m', due_back=date(2016, 10, 12))
BookInstance.objects.create(book=book2, imprint="Published March 1st 2011 by Penguin Group DAW Hardcover", status='a')
BookInstance.objects.create(book=book3, imprint="Published 1972 by DAW", status='a')
BookInstance.objects.create(book=book3, imprint="Published 1972 by DAW", status='a')

print("示例数据已添加！")